package com.jxtpro.area;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.kit.Ret;
import com.jxtpro.common.model.Area;
import com.jxtpro.common.service.BaseService;
/**
 * 地区服务
 * @author x
 *
 */
public class AreaService extends BaseService{

	public final static AreaService srv = new AreaService();
	final Area dao = new Area();
	
	/**
	 * 按pid查找地区
	 * @param pid
	 * @return
	 */
	public List<Area> findAreasByPid(String pid){
		List<Area> areas = dao.find("select name,cid,pid from area where pid=?",pid);
		return areas;
	}
	/**
	 * 按pid 查找，下级地区,统计地区数目
	 * @param pid
	 * @return
	 */
	public Ret findUnderAreas(String pid){
		List<Area> areas = findAreasByPid(pid);
		List<Integer> underCount = new ArrayList<Integer>();
		List<String> areaNames = new ArrayList<String>();
		for(int i = 0; i < areas.size(); i++){
			int count = findAreasByPid(areas.get(i).getCid()).size();
			underCount.add(count);
			areaNames.add("\""+areas.get(i).getName()+"\"");
		}
		Ret r = Ret.create("areas", areaNames);
		r.set("underCount", underCount);
		return r.set("count", r);
	}
}
